package com.example.shop_2211.detail

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.base.entity.AddEntityResult
import com.example.base.entity.CommentEntityResult
import com.example.base.entity.DetailEntityResult
import com.example.base.entity.ReplyEntityResult
import com.example.base.mvvm.BaseViewModel
import kotlinx.coroutines.launch

class DetailViewModel:BaseViewModel() {
    val detailSuccess = MutableLiveData<DetailEntityResult>()
    val detailFail = MutableLiveData<String>()
    fun detail(id:Int){
        viewModelScope.launch {
            try {
                detailSuccess.value = apiService.detail(id,"normal")
            }catch (e:Exception){
                detailFail.value = e.message
            }
        }
    }
    //添加购物车
    val addSuccess = MutableLiveData<AddEntityResult>()
    val addFail = MutableLiveData<String>()
    fun addCar(cartNum:Int,productAttrUnique:String,productId:Int){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,Any>()
                map["cartNum"] = cartNum
                map["productAttrUnique"] = productAttrUnique
                map["productId"] = productId
                addSuccess.value = apiService.addCar(mapToBody(map))
            }catch (e:Exception){
                addFail.value = e.message
            }
        }
    }
    val replySuccess = MutableLiveData<ReplyEntityResult>()
    val replyFail = MutableLiveData<String>()
    fun reply(id:Int,limit:Int,page:Int,type:String){
        viewModelScope.launch {
            try {
                replySuccess.value = apiService.getReply(id, limit, page, type)
            }catch (e:Exception){
                replyFail.value = e.message
            }
        }
    }

    val commentSuccess = MutableLiveData<CommentEntityResult>()
    val commentFail = MutableLiveData<String>()
    fun comment(){
        viewModelScope.launch {
            try {
                commentSuccess.value = apiService.getComment()
            }catch (e:Exception){
                commentFail.value = e.message
            }
        }
    }
}